//登陆模块


var loginApp = angular.module('loginApp',[]);
loginApp.controller('loginController',function ($scope,$http) {
    //我们创建数据
    $scope.formData = {};
    $scope.postForm = function () {
        //z这是我们提交表单的业务逻辑
        $scope.formData.action = 'login';
        //随后我们是用$http服务来向服务器发送一个请求
        $http({
            method:'POST',//发送方式
            url:'./get.php',//发送地址
            data:$.param($scope.formData),//发送的数据
            headers:{'Content-Type':'application/x-www-form-urlencoded'}//请求头

        }).success(function (data) {
            //console.log(data);
            //对数据的正确进行判断
            if(!data.success){
                if(!data.errors){
                    $scope.message = data.message;
                }else{
                    $scope.errorUsername = data.errors.username;
                    $scope.errorPassword = data.errors.password;
                }
            }else{
                //当密码正确的时候，跳转到列表页面
                window.location.href ='#/0';
            }
        })
    }
});

//列表模块
var pageList = angular.module('pageList',[]);
pageList.controller('ListTypeCtrl',function ($scope,$http) {
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
        
    }).success(function (data) {
        $scope.lists = data;
    }).error(function (data) {
        console.log('error...')
    })
})
total = 0;
//文章页面的列表
//创建arcListCtrl控制器拿到所有的文章
pageList.controller('arcListCtrl',function ($scope,$http,$location) {
    //首先获取当前的路径
    $scope.typeid = $location.path().replace('/','');
    //然后获取文章的总数量
    if($scope.typeid == 0){
        $get_total_url = 'get.php?action=get_total'
    }else{
        $get_total_url = 'get.php?action=get_total&where=typeid=' + $scope.typeid;
    }
    $http({
        method:'GET',
        url:$get_total_url
    }).success(function (data) {
        $scope.paginationConf.totalItems = data.total;
    }).error(function (data) {
        console.log('error...')
    })
    //获取文章的总数量之后，设置分页
    //获取文章的总数量之后，设置分页
    $scope.paginationConf = {
        //当前页数
        currentPage:1,
        //跟每页多少数据有关
        itemsPerPage:5,
        //每页的数据长度
        pagesLength:5,
        //这里添加一个S
        perPageOptions:[5,10, 20, 30, 40, 50],
        rememberPerPage:'perPageItems',
        onChange:function(){
            //获取分页的开始数
            if($scope.paginationConf.currentPage == 1){
                $scope.limit = 0;
            }else{
                //第一页的话,0 ,5 第二页 5,10,第三页 10,15
                $scope.limit = $scope.paginationConf.currentPage *
                    $scope.paginationConf.itemsPerPage -
                    $scope.paginationConf.itemsPerPage
            }
            //根据当前的type类型，显示不同的分类里面的文章页面
            //这里因为莫名的空格问题造成了URL的错误，要注意
            if($scope.typeid == 0){
                $get_url = 'get.php?action=get_list&offset=' + $scope.limit + '&rows=' + $scope.paginationConf.itemsPerPage + '&orderField=id&orderBy=DESC';
            }else{
                $get_url='get.php?action=get_list&offset='+$scope.limit+'&rows='+$scope.paginationConf.itemsPerPage+'&where=typeid='+$scope.typeid+'&orderField=id&orderBy=DESC';
            }
            //开始发送请求
            $http({
                method:'GET',
                url:$get_url
            }).success(function(data){
                $scope.lists = data;
            }).error(function(data){
                console.log('error...')
            })
        }
    }
    //删除开始
    $scope.del =function (index,Id) {
        //这里是删除开始的地方
        $scope.lists.splice(index,1);
        $http({
            method:'GET',
            url:'get.php?action=delete_article&id='+Id
        }).success(function (data) {
            console.log(data);
            if (data.code==101) {
                //删除成功
                console.log('删除成功');
                $scope.meg_success="删除成功！";
                $("#successbox").animate({opacity:'1'}).addClass("slideDown");
                setTimeout(function(){$("#successbox").removeClass().animate({opacity:'0'});}, 800);
                //setTimeout(function(){window.location.reload()},3000);
                $scope.paginationConf.totalItems --;
            } else {
                //删除失败
                console.log('删除失败');
                $scope.meg_success="";
                $scope.meg_error="删除失败! ";
                $("#errorbox").animate({opacity:'1'}).addClass("slideDown");
                setTimeout(function(){$("#errorbox").removeClass().animate({opacity:'0'});}, 800);
            }
        });
    };

    //时钟
    $scope.clock = new Date();
    var updateClock = function() {
        $scope.clock = new Date();
    };
    setInterval(function() {
        $scope.$apply(updateClock);
    }, 1000);
    updateClock();



    //过滤
    $scope.teamInfo = {};
    //如果我没有进行任何的选择，则要过滤的数据为空
    $scope.resetFilter = function(list) {
        if (!list) $scope.teamInfo = {};
    };

    //过滤数据的函数,参数1为数据，参数2为要查询的内容
    var uniqueValueInArray = function(arr,obj){
        var n = {},r = [];
        for(var i = 0;i < arr.length;i++){
            //循环所有的数据，每一条数据中，查询内容是否存在，如果存在则赋值给VAL，如果不存在，则返回原本的值
            //val返回的是每一条数据当中的某个相关字段的值
            var val = obj ? arr[i][obj] : arr[i];
            //如果没被定义
            if(!n[val]){
                n[val] = true;
                r.push(val);
            }
        }
        //将所有的数据当中的某个字段单独拿出来使用，例如场次啊，比分啊，等等
        return r;

    };

});

//新增

var addCont = angular.module('addCont',[]);
addCont.controller('AddContCtrl',function ($scope,$http) {
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function (data) {
        $scope.lists = data;
    }).error(function (data) {
        console.log('error...');
    })
    
    //第二部，执行写入操作
    $scope.formData = {};
    $scope.formData.action ='add_article';
    $scope.postForm = function () {
        $http({
            method:'POST',
            url:'get.php',
            data:$.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
            
        }).success(function (data) {
            $scope.errorBye = function () {
                $('#errorbox').fadeOut();
            }
            $scope.errorShow =function () {
                $('#errorbox').fadeIn();
            }
            if(!data.errors){
                //成功
                $scope.meg_success = '插入成功';
                $scope.meg_error = '';
                setTimeout(function () {
                    location.href = '#/0'
                },1000)
            }else{
                //失败
                $scope.meg_success = '';
                var get_error = '';
                if(data.errors.hasOwnProperty('title')){
                    get_error = data.errors.title;
                }
                if(data.errors.hasOwnProperty('content')){
                    get_error = data.errors.content;
                }
                if(data.errors.hasOwnProperty('typeid')){
                    get_error = data.errors.typeid;
                }
                $scope.meg_error = get_error;
            }
        })
    }

})

//修改模块
var modifyCont = angular.module('modifyCont',[]);
modifyCont.controller('ModifyContCtrl',function($scope,$http,$stateParams){
    //获取分类列表
    $http({
        method: 'GET',
        url: 'get.php?action=get_arctype&where=reid=0'
    }).success(function(data, status, headers, config) {
        $scope.types=data;
    }).error(function(data, status, headers, config) {
        console.log("get type list error...");
    });
    //读取这一条数据
    console.log($stateParams);
    $http({
        method: 'GET',
        url: 'get.php?action=get_article&id='+$stateParams.Id
    }).success(function(data, status, headers, config) {
        $scope.lists=data;
    }).error(function(data, status, headers, config) {
        console.log("error...");
    });
    //更新数据
    $scope.formData = {};
    $scope.postForm = function() {
        $scope.formData.action = 'update_article';
        $scope.formData.id = $stateParams.Id;
        $scope.formData.title = form.title.value;
        $scope.formData.content = form.content.value;
        $scope.formData.typeid = $("#typeid option:selected").val();//待优化取值方式
        $http({
            method  : 'POST',
            url     : 'get.php',
            data    : $.param($scope.formData),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
            .success(function(data) {
                console.log(data);
                if (data.code==101) {
                    //添加成功
                    console.log('修改成功');
                    $scope.meg_success="修改成功! 正在返回列表页...";
                    $scope.meg_error="";
                    setTimeout(function(){location.href='#/0'}, 1000);
                } else {
                    //添加失败
                    console.log('修改失败');
                    var get_errors="";
                    $scope.meg_success="";
                    //信息提示框状态
                    $scope.errorBye=function(){
                        $('#errorbox').fadeOut();
                    }
                    $scope.errorShow=function(){
                        $('#errorbox').fadeIn();
                        $scope.meg_error='';
                    }
                    if(data.errors){
                        console.log("有错误信息");
                        if(data.errors.hasOwnProperty('title')){
                            get_errors=data.errors.title;
                        }
                        if(data.errors.hasOwnProperty('content')){
                            get_errors=get_errors+(get_errors?"&":"")+data.errors.content;
                        }
                        $scope.meg_error=get_errors;
                    }else{
                        console.log("无错误信息");
                        $scope.meg_error="修改失败，再见了您勒";
                    }
                }
            });
    };
})


//查询
var showCont = angular.module('showCont',[]);
showCont.controller('showContCtrl',function ($http,$scope,$stateParams) {
    $http({
        method:'GET',
        url:'get.php?action=get_article&id=' + $stateParams.Id
    }).success(function (data) {
        $scope.lists = data;
    }).error(function (data) {
        console.log('error...')
    })
});